package com.eightywork.life.service;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

import com.eightywork.life.model.Space;
import com.eightywork.life.model.ToUserInfo;

public class CacheService extends BaseService {
	private final static String NAME = "space";
	private static Space space;
	private static Context context;
	private static boolean isSyncSinaWeibo;
	private static boolean isSyncTencentWeibo;
	private static boolean isSyncWeixin;
	private static ToUserInfo toUserInfo;
	public static void init(Context context) {
		CacheService.context = context;
		SharedPreferences pref = context.getSharedPreferences(NAME,
				Context.MODE_APPEND);
		Space space = new Space();
		CacheService.toUserInfo = new ToUserInfo();
		space.setmAuth(pref.getString("mAuth", ""));
		space.setXiaoquid(pref.getString("xiaoquid", "-1"));
		space.setXiaoquname(pref.getString("xiaoquname", ""));
		CacheService.space = space;
		CacheService.isSyncSinaWeibo = pref
				.getBoolean("isSyncSinaWeibo", false);
		CacheService.isSyncTencentWeibo = pref.getBoolean("isSyncTencentWeibo",
				false);
		CacheService.isSyncWeixin = pref.getBoolean("isSyncWeixin", false);
		
	}

	public static boolean isRookie() {
		SharedPreferences pref = context.getSharedPreferences("setting",
				Context.MODE_APPEND);
		return pref.getBoolean("isRookie", true);
	}

	public static void iamSuperman() {
		SharedPreferences pref = context.getSharedPreferences("setting",
				Context.MODE_APPEND);
		Editor edit = pref.edit();
		edit.putBoolean("isRookie", false);
		edit.commit();
	}

	public static void save(Space space) {
		SharedPreferences pref = context.getSharedPreferences(NAME,
				Context.MODE_APPEND);
		Editor edit = pref.edit();
		edit.putString("mAuth", space.getmAuth());
		edit.putString("xiaoquid", space.getXiaoquid());
		edit.putString("xiaoquname", space.getXiaoquname());
		edit.commit();
		CacheService.space = space;
	}

	public static void saveSyncWeibo(boolean sina, boolean tencent,
			boolean weixin) {
		SharedPreferences pref = context.getSharedPreferences(NAME,
				Context.MODE_APPEND);
		Editor edit = pref.edit();
		edit.putBoolean("isSyncSinaWeibo", sina);
		edit.putBoolean("isSyncTencentWeibo", tencent);
		edit.putBoolean("isSyncWeixin", weixin);
		edit.commit();
		CacheService.isSyncSinaWeibo = sina;
		CacheService.isSyncTencentWeibo = tencent;
		CacheService.isSyncWeixin = weixin;
	}
	
	public static void setLogin(String account, String password) {
		SharedPreferences pref = context.getSharedPreferences(NAME, Context.MODE_APPEND);
		Editor edit = pref.edit();
		edit.putString("account", account);
		edit.putString("password", password);
		edit.commit();
	}

	public static void saveProfile(Space profile) {
		Space space = CacheService.space;
		space.setBirthcity(profile.getBirthcity());
		space.setBirth(profile.getBirth());
		space.setIdcard(profile.getIdcard());
		space.setMarry(profile.getMarry());
		space.setMobile(profile.getMobile());
		space.setRealname(profile.getRealname());
		space.setSex(profile.getSex());
		space.setAvatar(profile.getAvatar());
		space.setQq(profile.getQq());
		space.setVerifystatus(profile.getVerifystatus());
		space.setVerifyType(profile.getVerifyType());
		space.setPrivacy(profile.getPrivacy());
		space.setUid(profile.getUid());
	//	System.out.println("===========>" + profile.getXiaoqu().toString());
		space.setPic_url(profile.getPic_url());
		space.setName(profile.getName());
		space.setNote(profile.getNote());
		space.setVerify_address(profile.getVerify_address());
		space.setQiname(profile.getQiname());
		space.setDongname(profile.getDongname());
		space.setCengname(profile.getCengname());
		space.setRoomname(profile.getRoomname());
		CacheService.space = space;
	}

	public static void clear() {
		SharedPreferences pref = context.getSharedPreferences(NAME,
				Context.MODE_APPEND);
		Editor edit = pref.edit();
		edit.clear();
		edit.commit();
		CacheService.space = new Space();
		CacheService.toUserInfo = new ToUserInfo();
	}

	public static boolean isLogined() {
		return !CacheService.space.getmAuth().equals("");
	}
	
	public static String getPassword() {
		SharedPreferences pref = context.getSharedPreferences(NAME,
				Context.MODE_APPEND);
		return pref.getString("password", "");
	}
	public static String getAccount() {
		SharedPreferences pref = context.getSharedPreferences(NAME,
				Context.MODE_APPEND);
		return pref.getString("account", "");
	}

	public static Space getSpace() {
		// Log.d("uid", space.getUid());
		return space;
	}
	
	public static ToUserInfo getToUserInfo() {
		return toUserInfo;
	}
	public static void setToUserInfo(ToUserInfo toUserInfo) {
		CacheService.toUserInfo = toUserInfo;
	}

	public static boolean isSyncSinaWeibo() {
		return isSyncSinaWeibo;
	}

	public static boolean isSyncWeixin() {
		return isSyncWeixin;
	}

	public static boolean isSyncTencentWeibo() {
		return isSyncTencentWeibo;
	}
}
